/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package at.tugraz.hidipic.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import javax.persistence.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 *
 * @author mario
 */
@Entity
@Table(name="user")
public class User implements UserDetails, Serializable {

    @Id
    private String username;
    private String password;
    private boolean enabled;
    private boolean accountNonLocked;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_customgrantedauthority")
    private Set<CustomGrantedAuthority> authorities;
    @OneToMany(mappedBy = "creator")
    private Set<Comment> comments;
    @OneToMany(mappedBy = "creator")
    private Set<Project> createdProjects;
    @OneToMany(mappedBy = "projectLeader")
    private Set<Project> leadProjects;
    @OneToMany(mappedBy = "creator")
    private Set<SubProject> createdSubProjects;
    @OneToMany(mappedBy = "creator")
    private Set<Result> results;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_subproject")
    private Set<SubProject> followSubProjects;
    @OneToMany(mappedBy = "creator")
    private Set<DataItem> createdDataItems;

    public Set<DataItem> getCreatedDataItems() {
        return createdDataItems;
    }

    public void setCreatedDataItems(Set<DataItem> createdDataItems) {
        this.createdDataItems = createdDataItems;
    }

    public Set<Project> getCreatedProjects() {
        return createdProjects;
    }

    public void setCreatedProjects(Set<Project> createdProjects) {
        this.createdProjects = createdProjects;
    }

    public Set<SubProject> getCreatedSubProjects() {
        return createdSubProjects;
    }

    public void setCreatedSubProjects(Set<SubProject> createdSubProjects) {
        this.createdSubProjects = createdSubProjects;
    }

    public Set<SubProject> getFollowSubProjects() {
        return followSubProjects;
    }

    public void setFollowSubProjects(Set<SubProject> followSubProjects) {
        this.followSubProjects = followSubProjects;
    }

    public Set<Project> getLeadProjects() {
        return leadProjects;
    }

    public void setLeadProjects(Set<Project> leadProjects) {
        this.leadProjects = leadProjects;
    }

    public Set<Result> getResults() {
        return results;
    }

    public void setResults(Set<Result> results) {
        this.results = results;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setAuthorities(Set<CustomGrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public Set<Comment> getComments() {
        return comments;
    }

    public void setComments(Set<Comment> comments) {
        this.comments = comments;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof User) {
            return this.username.equals(((User) obj).getUsername());
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return this.username.hashCode();
    }
}
